﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Jenzabar.Portal.Framework.Web.UI;//Req For PortletViewBase
namespace Portlet.AURoster
{
    public partial class Search_View : PortletViewBase
    {
        string fname, lname;




        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtLname.Text != "")
            {

                txtFname.Text = "";
                if (ddLname.SelectedItem.Text.Trim() == "")
                {
                    this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Select <b>Exact Match</b> or Begins With .");
                    return;

                }
                txtLname_TextChanged(this, e);

            }

            if (txtFname.Text != "")
            {

                txtLname.Text = "";
                if (ddFname.SelectedItem.Text.Trim() == "")
                {
                    this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Select <b>Exact Match</b> or Begins With .");
                    return;

                }
                txtFname_TextChanged(this, e);


            }

            if (txtFname.Text != "" && txtLname.Text != "")
            {
                if (ddFname.SelectedItem.Text.Trim() != "")
                {

                    fname = txtFname.Text.Trim();
                    lname = txtLname.Text.Trim();

                    SearchByName(lname, fname, ggSearch);
                }

            }

        }

        private void SearchByName(string lname, string fname, GridView gg)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
            {

                try
                {
                    conn.Open();

                    //SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                    //Using Scholarship Portlet Search
                    SqlDataAdapter da = new SqlDataAdapter("CUS_sp_SearchByFullName_Scholarship", conn);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@lname", SqlDbType.VarChar, 20));
                    da.SelectCommand.Parameters["@lname"].Value = lname;
                    da.SelectCommand.Parameters.Add(new SqlParameter("@fname", SqlDbType.VarChar, 20));
                    da.SelectCommand.Parameters["@fname"].Value = fname;
                    DataTable dt = new DataTable();



                    da.Fill(dt);
                    gg.DataSource = dt;
                    gg.DataKeyNames = new string[] { "ID_NUM" };
                    gg.DataBind();



                }
                catch (Exception ex)
                {

                    string errMsg = ex.Message;

                }
                finally
                {

                    conn.Close();
                }
            }
        }



        private void SearchByLname()
        {
            try
            {

                if (txtLname.Text != "")
                {
                    txtFname.Text = "";
                    lname = txtLname.Text.Trim();

                    if (lblLname.Text == "Contains")
                    {

                        string sqlContains = "CUS_sp_SearchByLnameContain_Scholarship";

                          SearchByLName(lname, ggSearch, sqlContains);


                    }
                    if (lblLname.Text == "Begin")
                    {
                        string sqlBegin = "CUS_sp_SearchByLnameBegin_Scholarship";

                      SearchByLName(lname, ggSearch, sqlBegin);


                    }
                    if (lblLname.Text == "Exact")
                    {

                        string sqlExact = "CUS_sp_SearchByLnameExact_Scholarship";


                      SearchByLName(lname, ggSearch, sqlExact);

                    }

                }
                txtLname.Focus();
            }
            catch (Exception ex)
            {
                this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Error retreiving Data Items: " + ex.Message);

            }

        }

        private void SearchByLName(string lname, GridView gg, string sp)
        {
            using (SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
            {

                try
                {
                    myConn.Open();

                    SqlCommand cmd = new SqlCommand(sp, myConn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@lname", SqlDbType.VarChar, 20).Value = lname;
                    SqlDataReader reader;
                    reader = cmd.ExecuteReader();
                    // Fill gV with Data
                    gg.DataSource = reader;
                    gg.DataKeyNames = new string[] { "ID" };
                    gg.DataBind();

                    //Close the reader
                    reader.Close();



                }
                catch (Exception ex)
                {

                    string res;

                    res = ex.Message;
                }
                finally
                {
                    // Close Connection
                    myConn.Close();
                }

            }
           
        }











        private void SearchByFname()
        {
            try
            {

                if (txtFname.Text != "")
                {
                    txtLname.Text = "";
                    fname = txtFname.Text.Trim();

                    if (lblFname.Text == "Contains")
                    {
                        string sqlContains = "CUS_sp_SearchByFnameContains_Scholarship";   // "Select ID_NUM,FIRST_NAME ,LAST_NAME, Middle_Name From Name_Master Where FIRST_NAME like '%' +@fname+ '%' ";// OR FIRST_NAME = @fname";
                      SearchByFName(fname, ggSearch, sqlContains);

                    }
                    if (lblFname.Text == "Begin")
                    {
                        string sqlBegin = "CUS_sp_SearchByFnameBegin_Scholarship";// "Select ID_NUM,FIRST_NAME ,LAST_NAME, Middle_Name From Name_Master Where FIRST_NAME like @fname+ '%'"; //OR LAST_NAME like %@lname%  ";
                       SearchByFName(fname, ggSearch, sqlBegin);

                    }
                    if (lblFname.Text == "Exact")
                    {

                        string sqlExact = "CUS_sp_SearchByFnameExact_Scholarship";//"Select ID_NUM,FIRST_NAME ,LAST_NAME, Middle_Name From Name_Master Where FIRST_NAME = @fname";
                        SearchByFName(fname, ggSearch, sqlExact);
                    }


                }
                txtFname.Focus();
            }
            catch (Exception ex)
            {
                this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Error retreiving Data Items: " + ex.Message);

            }
        }

        private void SearchByFName(string fname, GridView gg, string sp)
        {
            using (SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
            {

                try
                {
                    myConn.Open();

                    SqlCommand cmd = new SqlCommand(sp, myConn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@fname", SqlDbType.VarChar, 20).Value = fname;
                    SqlDataReader reader;
                    reader = cmd.ExecuteReader();
                    // Fill DV with Data
                    gg.DataSource = reader;
                    gg.DataBind();

                    //Close the reader
                    reader.Close();



                }
                catch (Exception ex)
                {

                    string res;

                    res = ex.Message;
                }
                finally
                {
                    // Close Connection
                    myConn.Close();
                }

            }
        }






        protected void ddLname_SelectedIndexChanged1(object sender, EventArgs e)
        {
            lblLname.Text = ddLname.SelectedValue.ToString();
        }

        protected void txtLname_TextChanged(object sender, EventArgs e)
        {
            //Get the value for lblLame Conatains,Exact or Begin
            ddLname_SelectedIndexChanged1(ddLname, e);
            SearchByLname();
        }

        protected void ddFname_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblFname.Text = ddFname.SelectedValue.ToString();
        }

        protected void txtFname_TextChanged(object sender, EventArgs e)
        {
            //Get the value for lblLame Conatains,Exact or Begin
            ddFname_SelectedIndexChanged(ddFname, e);

            SearchByFname();
        }



        protected void ggSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ggSearch.PageIndex = e.NewPageIndex;
            ggSearch.AllowPaging = true;
            btnSearch_Click(this, e);

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {

            this.ParentPortlet.NextScreen("Default");

            //GridViewRow row = ggSearch.SelectedRow;
            //Label noid = (Label)row.FindControl("lblNoId");
            //Label first = (Label)row.FindControl("lblFirst");
            //Label last = (Label)row.FindControl("lblLast");
            //this.ParentPortlet.PortletViewState["SearchNId"] = noid.Text;
            //Session["myID"] = noid.Text;
            //Session["Name"] = first.Text + ", " + last.Text;
            // Response.Write(Session["myID"].ToString());


        }

        protected void ggSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnSearch_Click(this, e);
        }
    }
}